/**
 * 
 */
package com.hcmu.view.MainHome;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import vanhoc.android.utils.Utils;

import com.telpoo.truyencuoi.R;
import com.hcmu.dto.DatabaseHelper;
import com.hcmu.dto.StoryDTO;
import com.hcmu.model.MainHomeModel;
import com.hcmu.network.utils.ConnectionDetector;
import com.hcmu.networklibs.rss.FeedSource;
import com.hcmu.networklibs.rss.RSSFeed;
import com.hcmu.networklibs.rss.RSSItem;
import com.hcmu.view.custome.RSSListAdapter;
import com.hcmu.view.custome.StoriesListAdapter;
import com.markupartist.android.widget.ActionBar;

import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.app.LauncherActivity.ListItem;
import android.content.Intent;
import android.content.pm.FeatureInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class OnlineMainView extends Activity {
	View app;
	ProgressDialog pgDialog;
	List<RSSItem> rssItemList;
	ListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LayoutInflater inflater = LayoutInflater.from(this);
		app = inflater.inflate(R.layout.layout_online_mainview, null);
		setContentView(app);

		ActionBar actionBar = (ActionBar) app.findViewById(R.id.actionbar);
		actionBar.setTitle(getResources().getString(R.string.title_activity_main_home));

		new showRssListTask().execute();

		final View[] children = new View[] { app };
		
		Utils.setupAds(this, ((ViewGroup)findViewById(R.id.rootAdView)));
	}

	private void initListView() {
		listView = (ListView) app.findViewById(R.id.listView);
		// adapter
		RSSListAdapter adapter = new RSSListAdapter(OnlineMainView.this, R.layout.story_item, rssItemList);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

				/*
				 * Intent it = new Intent(getApplicationContext(),
				 * ViewStoryDetailOnline.class); it.putExtra("STORY_TITLE",
				 * rssItemList.get(position).getTitle());
				 * it.putExtra("STORY_CONTENT",
				 * rssItemList.get(position).getDescription());
				 * startActivity(it);
				 */

				Intent it = new Intent(getApplicationContext(), ViewStoryDetailOnline.class);
				it.putExtra("title", rssItemList.get(position).getTitle());
				it.putExtra("page_url", rssItemList.get(position).getLink());
				startActivity(it);

				/*
				 * Toast.makeText(getApplicationContext(), "Title= " +
				 * rssItemList.get(position).getTitle()+ "\n" + "Link " +
				 * rssItemList.get(position).getLink() + "\n" + "cate " +
				 * rssItemList.get(position).getCategories() + "\n" +
				 * "content = " + rssItemList.get(position).getContent() + "\n"
				 * ,Toast.LENGTH_LONG).show();
				 */

			}

		});
	}

	public class showRssListTask extends AsyncTask<Void, Void, Void> {
		ProgressDialog pgDialog;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			pgDialog = ProgressDialog.show(OnlineMainView.this, "", "Loading...");
		}

		@Override
		protected Void doInBackground(Void... params) {
			FeedSource feedSource = new com.hcmu.networklibs.rss.HttpFeedSource();
			// get data to display to listview
			rssItemList = feedSource.getFeed();

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			if (pgDialog.isShowing()) {
				pgDialog.dismiss();
			}
			initListView();
		}
	}
}
